<template>
	<b-layout-index>
		<navbar :title="$t('选择商品')"></navbar>
		<!-- 		<u-sticky bgColor="#fff">
			<u-tabs current="current" @click="clickShop" :list="tablist"></u-tabs>
		</u-sticky> -->
		<view class="ltr" style="overflow: hidden;" v-if="datalist.length>0">
			<u-subsection :list="list" :current="currentSub" @change="sectionChange"></u-subsection>

		</view>
		<view class="">
			<shop-card-selected v-if="currentSub==0 && datalist.length>0" @action="selectedShop" :item="item"
				v-for="(item,index) in datalist" />
			<shop-taocan-index @onSelected="onTaoCanSelected" v-if="currentSub==1" />
		</view>

		<view class="bg-white sumview pd-10">

			<view class="sumbtn" :class="paymentAmount>=agentPrice?'':'dibalebtn'" @click="sendBtn">{{$t('下单')}} </view>

			<view class="sumbtn1">
				<view>
					<u--text :bold="true" size="16" mode="price" algin="center" :text="(paymentAmount)"></u--text>
					<u--text  size="12" type="info"  algin="center" :text="$t('算价格')"></u--text>
				</view>
				<view class="text-center">
					<u--text  type="info"  size="16" mode="price" algin="center" :text="(agentPrice)"></u--text>
						<u--text  size="12" type="info"  algin="center" :text="$t('代理费')"></u--text>
					
				</view>
			</view>
		</view>
		<u-modal :asyncClose="true" @confirm="confirm" :confirmText="$t('知道了')" :show="confirmFlag"
			:content="$t('对不起，您的消费金额不足规定的消费金额，需要补贴的消费金额{0}元',[agentPrice])"></u-modal>
	</b-layout-index>
</template>

<script>
	import {
		getFindByTabList
	} from '@/api/category.js'
	import {
		getFindByGoodsNewList
	} from '@/api/goods.js'
	export default {
		data() {
			return {
				confirmFlag: false,
				priceAll: 0,
				current: 0,
				tablist: [],
				agentId: 0,
				categoryId: 0,
				agentPrice: 0,
				datalist: [],
				shoplist: {},
				paymentAmount: 0,
				discountAmount: 0,
				totalAmount: 0,
				cloudType: 0,
				quantity: 0,
				upgrade: 0,
				activeInfo: false,
				list: [this.$t('商品'), this.$t('套餐')],
				// 或者如下，也可以配置keyName参数修改对象键名
				// list: [{name: '未付款'}, {name: '待评价'}, {name: '已付款'}],
				currentSub: 0
			}
		},
		onLoad: function(options) {
			console.log(options)
			uni.removeStorageSync("shopStorege")
			var cloudType = options.cloudType
			var agentId = options.agentId
			var agentPrice = options.price

			agentId = parseInt(agentId)

			if (isNaN(agentId)) {
				agentId = 0;
			}
			this.upgrade = options.upgrade;
			this.agentPrice = agentPrice
			this.agentId = agentId;
			this.cloudType = cloudType
			this.getFindByDataList()
			this.getFindByGoodsList()
		},
		methods: {
			onTaoCanSelected: function(item) {

				this.shoplist = {}
				this.paymentAmount = (item.paymentAmount);

				this.discountAmount = (item.discountAmount);
				this.totalAmount = (item.totalAmount);
				this.quantity = (item.quantity)

				this.activeInfo = true;
				var shopStorege = {
					data: item.details,
					priceAll: this.paymentAmount,
					agentPrice: this.agentPrice,
					agentId: this.agentId,
					paymentAmount: this.paymentAmount,
					discountAmount: this.discountAmount,
					totalAmount: item.totalAmount,
					type: 1,
					cloudType: this.cloudType,
					isUpgrade: this.upgrade,
					isActive: item.isActivty
				}
				uni.setStorageSync("shopStorege", shopStorege)

			},
			sectionChange(index) {
				this.shoplist = {}
				this.priceAll = 0;
				this.paymentAmount = 0;
				this.discountAmount = 0;
				this.totalAmount = 0;
				this.quantity = 0;
				this.currentSub = index;
			},
			clickShop: function(e) {
				console.log(e)
				this.categoryId = e.id
				this.getFindByGoodsList()
			},
			selectedShop: function(e) {
				var value = e.value;
				console.log("Value:" + value)
				var item = e.item;
				console.log("Item", item)
				this.paymentAmount = 0;
				this.discountAmount = 0;
				this.totalAmount = 0;
				this.quantity=0;
				console.log("Value:" + (item.price * value))
				var a = {
					isActive: item.isActive,
					price: item.price,
					paymentAmount: (item.price * value),
					discountAmount: ((item.marketPrice - item.price) * value),
					totalAmount: (item.marketPrice * value),
					name: item.name,
					image: item.image,
					quantity: item.quantity,
					count: value,
					goodsId: item.id,
					brandId: item.brandId,
					categoryId: item.categoryId
				}
				let ob = {}
				this.shoplist[item.id] = a
				for (let var1 in this.shoplist) {
					var ai = this.shoplist[var1]
					if (ai.count > 0) {
						ob[ai.isActive] = var1
					}
					//	this.priceAll = (value * item.price)
					this.paymentAmount = (this.paymentAmount + ai.paymentAmount);
					this.discountAmount = (this.discountAmount + ai.discountAmount);
					this.totalAmount = (this.totalAmount + ai.totalAmount);
					this.quantity = (this.quantity + ai.count)

				}
				let obVar = Object.keys(ob)
				console.log("selectedShop Keys", obVar.length)

				this.priceAll = (value * item.price)
				console.log("selectedShop", this.shoplist)
				console.log("selectedShop", Object.values(this.shoplist))
				if (obVar.length == 1) {
					this.activeInfo = true;
					var shopStorege = {
						data: Object.values(this.shoplist),
						priceAll: this.priceAll,
						agentPrice: this.agentPrice,
						agentId: this.agentId,
						paymentAmount: this.paymentAmount,
						discountAmount: this.discountAmount,
						totalAmount: this.totalAmount,
						type: 1,
						cloudType: this.cloudType,
						isUpgrade: this.upgrade,
						isActive: item.isActive,
						quantity:this.quantity
					}
					uni.setStorageSync("shopStorege", shopStorege)
				} else {
				
					this.activeInfo = false;
					uni.showModal({
						content: this.$t("活动货物不可选择选择活动货物或未被活动的货物选择"),
						showCancel: false,
						success: () => {

						}
					})
				}
			},
			getFindByDataList: function() {
				var the = this;

				getFindByTabList().then(res => {
					res.unshift({
						id: 0,
						name: this.$t("全部")
					})
					the.tablist = res
				})
			},
			getFindByGoodsList: function() {

				var the = this;
				var param = {
					agentId: the.agentId,
					categoryId: the.categoryId
				}
				getFindByGoodsNewList(param).then(res => {
					the.datalist = res
					if (res.length == 0) {
						the.currentSub = 1;
					}
				})
			},

			sendBtn: function() {
				//agentproductselectedpricevalid
				if (this.activeInfo) {
					if (this.paymentAmount >= this.agentPrice) {
						uni.navigateTo({
							url: "./buy"
						})
					} else {
						this.confirmFlag = true;
					}
				} else {
					uni.showModal({
						content: this.$t('活动货物不可选择 选择活动货物或未被活动的货物选择'),
						showCancel: false,
						success: () => {

						}
					})
				}
			},
			confirm: function() {
				this.confirmFlag = false;
			}
		}
	}
</script>

<style>
	.sumview {
		height: 50px;
		width: 100%;
		position: fixed;
		bottom: 0px;
		display: flex;
		align-items: center;
		z-index: 100;

	}

	.sumbtn {
		width: 100px;
		background-color: orange;
		padding-left: 10px;
		padding-right: 10px;
		padding-top: 5px;
		padding-bottom: 5px;
		text-align: center;
		border-radius: 30px;
		line-height: 30px;
		color: #fff;
		align-items: flex-start;
		align-content: flex-start;
	}
	.dibalebtn{
		background-color: #ccc;
	}

	.sumbtn1 {
		align-items: flex-end;
		align-content: flex-end;
		flex: 1;
		text-align: center;
		padding-left: 30px;
		padding-right: 10px;
		display: flex;
		justify-content: space-between;
	}
</style>